<?php 
namespace Home\Controller;
use Think\Controller;
/**
* 个人中心
*/
class CenController extends CommonController
{
	//设置页面
	public function index()
	{	
		$rec=D('user')->where("id='$_SESSION[id]'")->select();
		$rec=current($rec);
		$this->assign('arr',$rec);
		// var_dump($_SERVER);HTTP_HOST
		// var_dump($_SERVER['SERVER_ADDR']);
		$this->display();

		 
	}
	//退出
	public function quit()
	{
		session_unset();
		session_destroy();
		$this->success('操作完成',U('home/enter/index'));
	}
	// 修改数据
	public function mod()
	{

		$data=$_POST;
		if ($data['sex']==0) {
			$data['sex']='男';
		}else{
			$data['sex']='女';
		}
		$rec=D('user')->where("email='$data[email]'")->select();
		if ($rec) {
			$arr=array(
				'error'=>'1',
				'info' =>'邮箱已存在，请换一个邮箱',
				);
			echo json_encode($arr);
			exit;
		}
		$rec=D('user')->where("id='$_SESSION[id]'")->select();
		if ($data['email']!=$rec[0]['email']) {
			$data['act']=0;
		}
		// 写入数据库
		M('user')->where("id='$_SESSION[id]'")->save($data);
		$arr=array(
				'error'=>'0',
				'info' =>'修改成功',
				);
		$_SESSION['nikename']=$data['nikename'];
		echo json_encode($arr);
	}
	public function pass($value='')
	{	// 密码格式
		$data=$_POST;
		$preg='/^\w{6,16}$/';
		$c=preg_match($preg,$data['password']);
		if (!$c) {
			$arr=array(
				'error'=>'1',
				'info' =>'密码格式不正确',
				);
			echo json_encode($arr);
			exit;
		}
		if ($data['password']!=$data['repass']) {
			$arr=array(
				'error'=>'1',
				'info' =>'两次密码不一样，修改失败',
				);
			echo json_encode($arr);
			exit;
		}
		$data['password']=md5($data['password']);
		$rec=D('user')->where("id='$_SESSION[id]'")->select();
		if ($data['password']==$rec[0]['password']) {
			$arr=array(
				'error'=>'1',
				'info' =>'密码和原密码相同',
				);
			echo json_encode($arr);
			exit;
		}
		$arr=array(
				'error'=>'0',
				'info' =>'修改成功',
				);
		M('user')->where("id='$_SESSION[id]'")->save($data);
		echo json_encode($arr);
	}
	//用户中心
	public function user()
	{
		$rec=D('user')->where("id='$_SESSION[id]'")->order('create_time desc')->limit(10)->select();
		$rec=current($rec);
		$this->assign('arr',$rec);
		//问题
		// s
		$rse=D('question')->where("uid='$_SESSION[id]'")->order('create_time desc')->limit(10)->select();
		for ($i=0; $i <count($rse) ; $i++) { 
    		$rse[$i]['create_time']=date("Y-m-d H:i:s",$rse[$i]['create_time']);
    	}
					
		$this->assign('data',$rse);
		$this->display();
	}
	//我的消息news
	public function news()
	{
		$this->display();
	}
	//产品授权product
	public function product()
	{
		$this->display();
	}
	//我的主页hone
	public function hone()
	{	
		$id=I("get.id");
		// var_dump($id)
		if($id){
			$rec=M('user')->where("id='$id'")->select();
			// $sql='select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id';
			$sql = "select t1.create_time,t1.content,t2.title from answer t1 left join question t2 on t1.qid=t2.id where t1.uid='$id' order by t1.create_time desc lime limit 10";
			$rse=M()->query($sql);
			$q=M('question')->where("uid='$id'")->order('create_time desc')->limit(10)->select();

		}else{
			$rec=M('user')->where("id='$_SESSION[id]'")->select();
			$sql = "select t1.create_time,t1.content,t2.title from answer t1 left join question t2 on t1.qid=t2.id where t1.uid='$_SESSION[id]' order by t1.create_time desc limit 10";
			$rse=M()->query($sql);
			$q=M('question')->where("uid='$_SESSION[id]'")->order('create_time desc')->limit(10)->select();
			
		}
		$rec=current($rec);
		$rec['create_time']=getFormate($rec['create_time']);
		$this->assign('arr',$rec);

		for ($i=0; $i <count($rse) ; $i++) { 
	    		$rse[$i]['create_time']=getFormate($rse[$i]['create_time']);
	    	};
		$this->assign('data',$rse);

	    for ($i=0; $i <count($q) ; $i++) { 
	    		$q[$i]['create_time']=getFormate($q[$i]['create_time']);
	    	}
	    $this->assign('q',$q);	
	    // var_dump($q);
	    // exit;
		$this->display();
	}
	// 上传图片处理
	public function upload()
	{
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功
	    	$info = current($info);
	    	$path ="Uploads/".$info['savepath'].$info['savename'];
	    	// var_dump($path);
	    	// exit;

	    	$sec=M('user')->where("id='$_SESSION[id]'")->setField('face',$path);
	    	if ($sec) {
	    		echo $path;
	    	}else{
	    		echo 0;
	    	}
	        
    }
	}
}

 ?>